{include file="common/pc/header" title="企业列表" /}

<style>
    #enterprises-table img {
        width: 60px;
        height: 60px;
    }

    .layui-form-item.secret .layui-input-block {
        width: calc(100% - 180px);
    }

    .layui-form-item.secret .switch {
        position: relative;
        float: right;
        display: block;
        margin-top: -40px;
    }

    #enterprises-table .layui-btn {
        width: 100px;
    }
</style>
<script>
    window.reload_when_tab_focus = true;
</script>
<div id="enterprises">
    <button class="layui-btn" onclick="add()">新增企业</button>
    <form action="#" class="layui-form">
        <table class="layui-table" id="enterprises-table">
            <colgroup>
                <col width="20">
                <col width="50">
                <col width="120">
                <col width="70">
                <col width="70">
                <col width="70">
                <col width="300">
            </colgroup>
            <thead>
            <tr>
                <th>ID</th>
                <th>Logo</th>
                <th>企业名称</th>
                <th>联系人</th>
                <th>联系电话</th>
                <th>企业备注</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            {volist name="enterprises" id="enterprise"}
            <tr>
                <td>
                    {$enterprise.id}
                </td>
                <td>
                    <img src="{$enterprise.logo}" onerror="this.src='__IMAGES__/enterprise.png';"/>
                </td>
                <td>
                    {$enterprise.title}
                </td>
                <td>
                    {$enterprise.contacter}
                </td>
                <td>
                    {$enterprise.phone}
                </td>
                <td>
                    {$enterprise.description}
                </td>
                <td>
                    <button class="layui-btn layui-btn-sm layui-btn-success"
                            onclick="openEnterpriseDetailDlg('{$enterprise.title}','{$enterprise.id}')">
                        授权模块
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal"
                            data-enterprise='{php}echo json_encode($enterprise);{/php}'
                            onclick="edit(this)">
                        编辑企业
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-primary"
                            onclick="mockEnterprise('{$enterprise.id}')">
                        企业后台
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger"
                            onclick="deleteEnterprise('{$enterprise.title}','{$enterprise.id}')">
                        删除企业
                    </button>
                    {if condition="$enterprise['corpid']"}
                    <button class="layui-btn layui-btn-sm layui-btn-primary"
                            onclick="microAppManage('{$enterprise.title}','{$enterprise.id}')">
                        微应用管理
                    </button>
                    {/if}
                </td>
            </tr>
            {/volist}
            </tbody>
        </table>
    </form>
</div>

<!-- 编辑对话框 -->
<div id="editDlgTpl" style="display: none;padding-right: 20px;">
    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">企业名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="edit_title"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Logo</label>
            <div class="layui-input-block">
                {d:up type="image" btn="false" field="logo" id="edit_logo" w="60px" h="60px" id="edit_logo" }上传图片{/d:up}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="edit_contacter"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="edit_phone"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="edit_description"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">corpid</label>
            <div class="layui-input-block">
                <input type="text" id="edit_corpid" placeholder="请输入corpid" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item secret">
            <label class="layui-form-label">cropsecret</label>
            <div class="layui-input-block">
                <input type="password" id="edit_corpsecret" placeholder="请输入cropsecret"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="switch">
                <input type="checkbox" lay-skin="switch" lay-filter="corpsecret" lay-text="显示|隐藏"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">appid</label>
            <div class="layui-input-block">
                <input type="text" id="edit_appid" placeholder="请输入appid" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item secret">
            <label class="layui-form-label">appsecret</label>
            <div class="layui-input-block">
                <input type="password" id="edit_appsecret" placeholder="请输入appsecret" autocomplete="off"
                       class="layui-input">
            </div>
            <div class="switch">
                <input type="checkbox" lay-skin="switch" lay-filter="appsecret" lay-text="显示|隐藏"/>
            </div>
        </div>
    </div>
</div>

<!-- 新增 -->
<div id="addDlgTpl" style="display: none;padding-right: 20px;">
    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">企业名称</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="add_title"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Logo</label>
            <div class="layui-input-block">
                {d:up type="image" btn="false" field="logo" id="add_logo" w="60px" h="60px" id="add_logo" }上传图片{/d:up}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="add_contacter"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="add_phone"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="add_description"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">corpid</label>
            <div class="layui-input-block">
                <input type="text" id="add_corpid" placeholder="请输入corpid" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item secret">
            <label class="layui-form-label">cropsecret</label>
            <div class="layui-input-block">
                <input type="password" id="add_corpsecret" placeholder="请输入cropsecret"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="switch">
                <input type="checkbox" lay-skin="switch" lay-filter="add_corpsecret" lay-text="显示|隐藏"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">appid</label>
            <div class="layui-input-block">
                <input type="text" id="add_appid" placeholder="请输入appid" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item secret">
            <label class="layui-form-label">appsecret</label>
            <div class="layui-input-block">
                <input type="password" id="add_appsecret" placeholder="请输入appsecret" autocomplete="off"
                       class="layui-input">
            </div>
            <div class="switch">
                <input type="checkbox" lay-skin="switch" lay-filter="add_appsecret" lay-text="显示|隐藏"/>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'element'], function () {
        var form = layui.form;

        form.on('switch(corpsecret)', function (data) {
            $("#edit_corpsecret").attr('type', data.elem.checked ? 'text' : 'password');
        });
        form.on('switch(appsecret)', function (data) {
            $("#edit_appsecret").attr('type', data.elem.checked ? 'text' : 'password');
        });
        form.on('switch(add_corpsecret)', function (data) {
            $("#add_corpsecret").attr('type', data.elem.checked ? 'text' : 'password');
        });
        form.on('switch(add_appsecret)', function (data) {
            $("#add_appsecret").attr('type', data.elem.checked ? 'text' : 'password');
        });
    });


    /**
     * 打开企业详情tab
     *
     * @param title
     * @param eid
     */
    function openEnterpriseDetailDlg(title, eid) {
        var url = "/core/site.enterprise/detail?eid=" + eid;
        layer.open({
            type: 2,
            title: title,
            shadeClose: true,
            shade: false,
            maxmin: false,
            area: ['80vw', '80vh'],
            resize: true,
            content: url
        });
    }

    /**
     * 企业后台mock
     */
    function mockEnterprise(eid) {
        var link = "/core/site.enterprise/mock?eid=" + eid;
        window.open(link);
    }

    /**
     * 删除企业
     */
    function deleteEnterprise(title, eid) {
        layer.confirm("您将要删除企业【" + title + "】，<b style='color: red;'>删除后企业将无法登录和使用本系统</b>，确认继续？", function () {
            layer.prompt({
                formType: 0,
                value: '',
                title: '请输入企业名称【' + title + '】再次确认'
            }, function (value, index, elem) {
                if (value != title) {
                    layer.alert("输入错误");
                    return;
                }
                g.loading();
                $.post("delete", {id: eid}, function (ret) {
                    g.loading(false);
                    if (ret.code == 0) {
                        layer.alert(ret.msg);
                    } else {
                        layer.msg("企业删除成功，页面刷新中...");
                        document.location.reload();
                    }
                }, "json");
            });
        });
    }

    /**
     * 微应用管理
     */
    function microAppManage(title, eid) {
        layer.open({
            type: 2,
            title: title + " - 微应用管理",
            shadeClose: true,
            shade: false,
            maxmin: false,
            area: ['60vw', '80vh'],
            resize: true,
            content: "/core/site.enterprise_microapp/all?eid=" + eid
        });
    }

    /**
     * 编辑企业信息
     *
     * @param target
     */
    function edit(target) {
        var height = (window.frameElement ? $(window.frameElement).height() : $(window).height()) - 50;
        if (height > 600) {
            height = 600;
        }

        var enterprise = eval("(" + $(target).attr("data-enterprise") + ")");
        $("#edit_title").val(enterprise.title);
        $("#edit_contacter").val(enterprise.contacter);
        $("#edit_phone").val(enterprise.phone);
        $("#edit_description").val(enterprise.description);
        $("#edit_corpid").val(enterprise.corpid);
        $("#edit_corpsecret").val(enterprise.corpsecret);
        $("#edit_appid").val(enterprise.appid);
        $("#edit_appsecret").val(enterprise.appsecret);
        g.up.set("edit_logo", enterprise.logo);
        layer.open({
            type: 1,
            title: '编辑企业信息',
            content: $("#editDlgTpl"),
            area: ['60vw', height + "px"],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var row = {};
                row.id = enterprise.id;
                row.title = $("#edit_title").val();
                row.contacter = $("#edit_contacter").val();
                row.phone = $("#edit_phone").val();
                row.description = $("#edit_description").val();
                row.logo = g.up.get("edit_logo");
                row.corpid = $("#edit_corpid").val();
                row.corpsecret = $("#edit_corpsecret").val();
                row.appid = $("#edit_appid").val();
                row.appsecret = $("#edit_appsecret").val();
                if (!row.title) {
                    layer.alert("请输入企业名称");
                    return;
                }
                g.loading();
                $.post("edit", row, function (ret) {
                    g.loading(false);
                    if (ret.code == 0) {
                        layer.alert(ret.msg);
                    } else {
                        layer.msg("企业信息更新成功，页面刷新中...");
                        document.location.reload();
                    }
                }, "json");
                return true;
            }
        });
    }

    /**
     * 增加企业
     */
    function add() {
        var height = (window.frameElement ? $(window.frameElement).height() : $(window).height()) - 50;
        if (height > 600) {
            height = 600;
        }

        layer.open({
            type: 1,
            title: '新增企业',
            content: $("#addDlgTpl"),
            area: ['60vw', height + "px"],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var row = {};
                row.title = $("#add_title").val();
                row.contacter = $("#add_contacter").val();
                row.phone = $("#add_phone").val();
                row.description = $("#add_description").val();
                row.logo = g.up.get("add_logo");
                row.corpid = $("#add_corpid").val();
                row.corpsecret = $("#add_corpsecret").val();
                row.appid = $("#add_appid").val();
                row.appsecret = $("#add_appsecret").val();
                if (!row.title) {
                    layer.alert("请输入企业名称");
                    return;
                }
                g.loading();
                $.post("add", row, function (ret) {
                    g.loading(false);
                    if (ret.code == 0) {
                        layer.alert(ret.msg);
                    } else {
                        layer.msg("企业添加成功，页面刷新中...");
                        document.location.reload();
                    }
                }, "json");
                return true;
            }
        });
    }
</script>

{include file="common/pc/footer"/}